package com.anlogic.sdk.efuse.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.anlogic.sdk.efuse.model.AuthType;
import com.anlogic.sdk.efuse.model.EncType;
import com.anlogic.sdk.efuse.model.GroupId;
import com.anlogic.sdk.efuse.model.ProgramEfuseModel;
import com.anlogic.sdk.efuse.ui.ProgramEfuseDialog;
import com.anlogic.sdk.efuse.utils.ProgramEfuseUtils;

public class ProgramEfuseHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		ProgramEfuseDialog dialog = new ProgramEfuseDialog(window.getShell());
		int ret = dialog.open();
		if (ret == ProgramEfuseDialog.OK) {
			runProgram(dialog.getModel());
		}
		return null;
	}
	
	public void runProgram(ProgramEfuseModel model) {
		writeScript(model);
		ProgramEfuseUtils.programEfuse();
	}
	
	public void writeScript(ProgramEfuseModel model) {
		String encKeyHash = model.getEncKeyHashValue(GroupId.FIRST);
		String encKey2Hash = model.getEncKeyHashValue(GroupId.SECOND);
		String pubKeyHash = model.getPpkHashValue(GroupId.FIRST);
		String pubKey2Hash = model.getPpkHashValue(GroupId.SECOND);

		int encType = EncType.getIdByName(model.getEncType(GroupId.FIRST));
		int authType = AuthType.getIdByName(model.getAuthType(GroupId.FIRST));
		
		boolean encKeyWriteLock = model.getEncKeyWriteLock(GroupId.FIRST);
		boolean encKey2WriteLock = model.getEncKeyWriteLock(GroupId.SECOND);
		boolean ppkWriteLock = model.getPpkWriteLock(GroupId.FIRST);
		boolean ppk2WriteLock = model.getPpkWriteLock(GroupId.SECOND);
		boolean revokeEncKey = model.getRevokeFirstEncKey();
		boolean revokePpk = model.getRevokeFirstPpk();
		
		boolean trngWrite = model.getTRNGWrite();

		String encKeyCmd = "";
		String pubKeyCmd = "";
		String encTypeCmd = "";
		String authTypeCmd = "";
		String encKeyWrLkCmd = "";
		String pubKeyWrLkCmd = "";
		String encKeyRevokeCmd = "";
		String pubKeyRevokeCmd = "";
		
		String trngWriteCmd = "";
		
		String cmd = "";
		
		if (encKeyHash != null && !encKeyHash.isEmpty() && encKey2Hash != null && !encKey2Hash.isEmpty()) {
			encKeyCmd = ProgramEfuseUtils.getEncKeyCmd(encKeyHash, encKey2Hash);
		} else if (encKeyHash != null && !encKeyHash.isEmpty()) {
			encKeyCmd = ProgramEfuseUtils.getEncKeyCmd(encKeyHash);
		}
		cmd = concatCmd(cmd, encKeyCmd);

		if (pubKeyHash != null && !pubKeyHash.isEmpty() && pubKey2Hash != null && !pubKey2Hash.isEmpty()) {
			pubKeyCmd = ProgramEfuseUtils.getPubKeyCmd(pubKeyHash, pubKey2Hash);
		} else if (pubKeyHash != null && !pubKeyHash.isEmpty()) {
			pubKeyCmd = ProgramEfuseUtils.getPubKeyCmd(pubKeyHash);
		}
		cmd = concatCmd(cmd, pubKeyCmd);
		
		if (!encKeyCmd.isEmpty()) {
			if (encType > 0) {
				encTypeCmd = ProgramEfuseUtils.getEncTypeCmd(encType);
			}
			cmd = concatCmd(cmd, encTypeCmd);
			
			if (encKeyWriteLock && encKey2WriteLock) {
				encKeyWrLkCmd = ProgramEfuseUtils.getEncKeyWriteLockCmd(encKeyWriteLock, encKey2WriteLock);
			} else if (encKeyWriteLock) {
				encKeyWrLkCmd = ProgramEfuseUtils.getEncKeyWriteLockCmd(encKeyWriteLock);
			}
			cmd = concatCmd(cmd, encKeyWrLkCmd);

			if (revokeEncKey) {
				encKeyRevokeCmd = ProgramEfuseUtils.getEncKeyRevokeCmd(revokeEncKey);
			}
			cmd = concatCmd(cmd, encKeyRevokeCmd);
		}

		if (!pubKeyCmd.isEmpty()) {
			if (authType > 0) {
				authTypeCmd = ProgramEfuseUtils.getAuthTypeCmd(authType);
			}
			cmd = concatCmd(cmd, authTypeCmd);
			
			if (ppkWriteLock && ppk2WriteLock) {
				pubKeyWrLkCmd = ProgramEfuseUtils.getPubKeyWriteLockCmd(ppkWriteLock, ppk2WriteLock);
			} else if (ppkWriteLock) {
				pubKeyWrLkCmd = ProgramEfuseUtils.getEncKeyWriteLockCmd(ppkWriteLock);
			}
			cmd = concatCmd(cmd, pubKeyWrLkCmd);

			if (revokePpk) {
				pubKeyRevokeCmd = ProgramEfuseUtils.getPubKeyRevokeCmd(revokePpk);
			}
			cmd = concatCmd(cmd, pubKeyRevokeCmd);
		}
		
		if (trngWrite) {
			trngWriteCmd = ProgramEfuseUtils.getTRNGWriteCmd(trngWrite);
			cmd = concatCmd(cmd, trngWriteCmd);
		}

		cmd += "exit\n";
		
		ProgramEfuseUtils.writeScript(cmd);
	}
	
	public String concatCmd(String cmd, String added) {
		if (!added.isEmpty()) {
			cmd = cmd + added + "\n";
		}
		return cmd;
	}
}
